﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Diagnostics;

namespace MySCM.Agent.Log
{
    // Static Log Agent - 
    // - One per App
    // - Append logs under \Logs folder for each App
    // - LogType: Design, Error, and User
    public class LogAgent
    {
        static FileStream fs;
        static string desLogFilename;
        static string errLogFilename;
        static string usrLogFilename;


        public LogAgent(string logRoot)
        {
            desLogFilename = logRoot + @"Design.log";
            errLogFilename = logRoot + @"Error.log";
            usrLogFilename = logRoot + @"User.log";
            if (!Directory.Exists(logRoot))
            {
                Directory.CreateDirectory(logRoot);
            }
        }


        public void WriteLog(string logType, string level, string logMessage)
        {
            try
            {
                switch (logType.ToUpper())
                {
                    case "DESIGN":
                        fs = new FileStream(desLogFilename, FileMode.Append);
                        break;
                    case "ERROR":
                        fs = new FileStream(errLogFilename, FileMode.Append);
                        break;
                    case "USER":
                        fs = new FileStream(usrLogFilename, FileMode.Append);
                        break;
                    default:
                        fs = new FileStream(desLogFilename, FileMode.Append);
                        break;
                }
                StreamWriter sw = new StreamWriter(fs);
                StackFrame sf = new StackFrame(1, true);
                sw.WriteLine(DateTime.Now.ToString() + " *** " + level + " *** " + sf.GetFileName() + " (" + sf.GetFileLineNumber() + ") *** " + sf.GetMethod() + " *** " + logMessage);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

    }
}
